package com.hema.auction.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hema.auction.R;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.bean.ProfileInfo;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Constant;
import com.hema.auction.utils.Utils;
import com.hema.auction.utils.manager.ScreenManager;
import com.hema.auction.widget.view.TitleView;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 描述:设置密码(已废弃)
 * 作者:john
 */
public class SetPwdActivity extends BaseActivity {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.ll_pwd)
    LinearLayout llPwd;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    private String phone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_pwd);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        phone = getIntent().getStringExtra(Constant.EXTRA_PHONE);
    }

    @OnClick({R.id.tv_ok})
    public void onClick(View v) {
        if (Utils.isEmpty(etPwd.getText().toString())) {
            showToastSafe(R.string.pwd_isempty);
        } else if (etPwd.getText().toString().length() < 6 || etPwd.getText().toString().length() > 18) {
            showToastSafe(R.string.pwd_error);
        } else {
            showProgressDialog(R.string.login_now);
            HttpManager.getInstance(this).register(phone, Utils.stringToMD5(etPwd.getText().toString().trim()), this);
        }
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_REGISTER:
                try {
                    if (result.getInt("errcode") == 0) {
                        spManager.setIsLogin(true);
                        profileInfo = new Gson().fromJson(result.getJSONObject("data").toString(), ProfileInfo.class);
                        setAlisa();
                        updateProfile();
                        spManager.setUserId(profileInfo.getId());
                        ScreenManager.getInstance().popAllActivitys();
                        intentTo(MainActivity.class);

                    }
                    showToastSafe(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

}
